import 'package:flutter/material.dart';
import 'package:flutter_bmfmap_example/http/http.dart';
import 'package:flutter_bmfmap_example/pages/marketpage/pages/addresspage/addrespage.dart';
import 'package:flutter_bmfmap_example/pages/marketpage/pages/submitOrders/submitOrderCart/submitOrderCartL.dart';

// void main() => runApp(MyApp());

class SubmitOrders extends StatefulWidget {
  SubmitOrders({Key key}) : super(key: key);

  @override
  _SubmitOrdersState createState() => _SubmitOrdersState();
}

class _SubmitOrdersState extends State<SubmitOrders> {
  String orderNumber = '';
  List orderBy = [];
  var absoluteAddress;
  // List buyList = [
  //   {
  //     "id": 1,
  //     "commodityId": 1,
  //     "name": "新1级智能空调",
  //     "intro": "新一级能效,温湿双控,自动清洁,静音设计,智能防直吹,全直流变频,智能互联,个性除湿",
  //     "money": 2199,
  //     "commodityNumber": 2,
  //     "imgurl1":
  //         "http://xiangrikui.resource.vaiwan.com/2021/07/21/62d52004-a5d0-40fa-943f-bc17ef2b5f6b.png"
  //   },
  //   {
  //     "id": 2,
  //     "commodityId": 2,
  //     "name": "全自动智能门锁",
  //     "intro": "高端全自动摄像头门锁 门锁、门铃  摄像头",
  //     "money": 2199,
  //     "commodityNumber": 2,
  //     "imgurl1":
  //         "http://xiangrikui.resource.vaiwan.com/2021/07/21/dfc6e499-6f02-4c31-8eb0-5bba0bf14101.png"
  //   },
  //   // {
  //   //   "id": 3,
  //   //   "commodityId": 3,
  //   //   "name": "洗烘一体机 ",
  //   //   "intro":
  //   //       "「洗衣机新品特惠！到手价仅2399元！」「健康银离子，微蒸空气洗，健康除螨洗，热卖中...」安静洗涤│健康银离子│微蒸空气洗│健康除螨洗",
  //   //   "money": 2399,
  //   //   "commodityNumber": 2,
  //   //   "imgurl1":
  //   //       "http://xiangrikui.resource.vaiwan.com/2021/07/21/b6707132-3168-4fea-ab35-4b024a80c120.png"
  //   // }
  // ];

  TextEditingController _textValue = new TextEditingController(text: '');

  @override
  initState() {
    super.initState();
    // 页面加载时调用 用来开始时访问数据库
    _getinfo();
  }

  _getinfo() async {
    var response = await Http.get(
        'http://xiangrikui.vaiwan.com/userAddress/getAbsoluteAddress');
    this.absoluteAddress = response;
    // print(this.absoluteAddress['name']);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('订单确认'),
        centerTitle: true,
        backgroundColor: Colors.transparent,
      ),
      body: Container(
        color: Colors.grey[100],
        child: Column(
          children: [
            InkWell(
              onTap: () {
                Navigator.of(context).push(MaterialPageRoute(
                  builder: (context) {
                    return AddressPage(); //返回的是需要跳转单页面
                  },
                )).then((value) => _getinfo());
              },
              child: Container(
                margin: EdgeInsets.all(16),
                padding: EdgeInsets.all(12),
                // color: Colors.blue[100],
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(10)),
                child: Row(
                  children: [
                    // Image.asset("images/confirmOrder/定位.png"),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Row(
                          children: [
                            Container(
                              margin: EdgeInsets.only(right: 12),
                              child: Text(
                                this.absoluteAddress == null
                                    ? "---"
                                    : this.absoluteAddress['name'],
                                style: TextStyle(),
                              ),
                            ),
                            Text(
                              this.absoluteAddress == null
                                  ? "---"
                                  : this.absoluteAddress['phone'],
                              style: TextStyle(
                                  fontSize: 12, color: Colors.grey[600]),
                            )
                          ],
                        ),
                        Row(
                          children: [
                            Container(
                              width: 232,
                              child: Text(
                                this.absoluteAddress == null
                                    ? "---"
                                    : this.absoluteAddress['province'] +
                                        ' ' +
                                        this.absoluteAddress['municipality'] +
                                        ' ' +
                                        this.absoluteAddress['district'] +
                                        ' ' +
                                        this.absoluteAddress['addressIntro'],
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                                style: TextStyle(
                                    fontSize: 12, color: Colors.black54),
                              ),
                            ),
                            // Spacer(),
                          ],
                        ),
                      ],
                    ),
                    Container(
                      margin: EdgeInsets.only(left: 40),
                      // color: Colors.red,
                      child: Icon(
                        Icons.keyboard_arrow_right_sharp,
                        color: Colors.grey[600],
                      ),
                      // onTap: () {
                      //   print('进入收货地址选择页面');
                      // },
                    ),
                  ],
                ),
              ),
            ),
            Expanded(
              child: ListView(
                  children:
                      // submitOrderCartList(),
                      submitOrderCartList()),
            )
          ],
        ),
      ),
      bottomNavigationBar: Container(
        padding: EdgeInsets.all(10),
        height: 140,
        child: Column(
          children: [
            Row(
              // mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  margin: EdgeInsets.only(right: 16),
                  padding: EdgeInsets.only(top: 10),
                  child: Text('订单备注'),
                ),
                Container(
                  width: 230,
                  child: TextField(
                    onChanged: (value) {
                      // print(value);
                      // change();
                    },
                    controller: _textValue,
                    maxLines: 3,
                    maxLength: 60,
                    style: TextStyle(fontSize: 12, color: Colors.black54),
                    cursorColor: Colors.black,
                    cursorWidth: 1.5,
                    keyboardType: TextInputType.emailAddress,
                    decoration: InputDecoration(
                        border: InputBorder.none,
                        hintText: '选填，请自行填写',
                        hintStyle:
                            TextStyle(fontSize: 12, color: Colors.grey[800])),
                  ),
                ),
              ],
            ),
            Container(
              // padding: EdgeInsets.all(10),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Text(
                    '共${getAllNum()}件',
                    style: TextStyle(color: Colors.grey[700], fontSize: 13),
                  ),
                  Container(
                    margin: EdgeInsets.fromLTRB(8, 0, 4, 0),
                    child: Text(
                      '共计:',
                      style: TextStyle(),
                    ),
                  ),
                  Text(
                    '￥${getAllMoney()}',
                    style: TextStyle(color: Colors.deepOrange[700]),
                  ),
                  InkWell(
                    onTap: () {
                      print('object');
                      postShopBuy();
                      _showbuypage();
                    },
                    child: Container(
                      padding: EdgeInsets.fromLTRB(12, 6, 12, 6),
                      margin: EdgeInsets.only(left: 8),
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(20),
                          color: Colors.orange,
                          gradient: LinearGradient(
                              begin: Alignment.topLeft,
                              end: Alignment.bottomRight,
                              // stops: [0.0,1.0],
                              colors: [Colors.pink, Colors.red])),
                      child: InkWell(
                        child: Text(
                          '提交订单',
                          style: TextStyle(color: Colors.white, fontSize: 13),
                        ),
                      ),
                    ),
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  _showbuypage() {
    showModalBottomSheet(
        context: context,
        isScrollControlled: true, //一：设为true，此时为全屏展示
        shape: RoundedRectangleBorder(
          // borderRadius: BorderRadius.circular(25),
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(25), topRight: Radius.circular(25)),
        ),
        builder: (BuildContext context2) {
          return StatefulBuilder(builder: (context2, setBottomSheetState) {
            return Container(
              padding: EdgeInsets.all(16),
              // height: 10,
              child: Container(
                // height: 500,
                decoration: BoxDecoration(
                    border: Border.all(color: Colors.grey[300], width: 0.5),
                    borderRadius:
                        BorderRadius.vertical(top: Radius.circular(50)),
                    boxShadow: [
                      BoxShadow(
                          color: Colors.grey[50],
                          offset: Offset(0, -2),
                          blurRadius: 0.1,
                          spreadRadius: 1)
                    ]),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Container(
                      margin: EdgeInsets.only(bottom: 8),
                      child: Column(
                        children: [
                          Text(
                            '订单号',
                            style: TextStyle(
                                fontWeight: FontWeight.bold, fontSize: 16),
                          ),
                          Text(
                            this.orderNumber,
                          ),
                        ],
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Row(
                        children: [
                          Text(
                            '收货人:',
                            style: TextStyle(fontWeight: FontWeight.bold),
                          ),
                          Padding(
                            padding: const EdgeInsets.fromLTRB(10, 0, 8, 0),
                            child: Container(
                              width: 100,
                              height: 24,
                              child: Text(this.absoluteAddress['name'],
                                  style: TextStyle(
                                      fontSize: 16, color: Colors.grey[400])),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Row(
                        children: [
                          Text(
                            '电    话:',
                            style: TextStyle(fontWeight: FontWeight.bold),
                          ),
                          Padding(
                            padding: const EdgeInsets.fromLTRB(10, 0, 8, 0),
                            child: Container(
                              width: 130,
                              height: 24,
                              child: Text(
                                this.absoluteAddress['phone'],
                                style: TextStyle(
                                    fontSize: 16, color: Colors.grey[400]),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Row(
                        children: [
                          Text(
                            '地    址：',
                            style: TextStyle(fontWeight: FontWeight.bold),
                          ),
                          Padding(
                            padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
                            child: Container(
                              width: 210,
                              // height: 20,
                              child: Text(
                                this.absoluteAddress['province'] +
                                    ' ' +
                                    this.absoluteAddress['municipality'] +
                                    ' ' +
                                    this.absoluteAddress['district'] +
                                    ' ' +
                                    this.absoluteAddress['addressIntro'],
                                style: TextStyle(
                                    fontSize: 16, color: Colors.grey[400]),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Row(
                        children: [
                          Text(
                            '订单备注：',
                            style: TextStyle(fontWeight: FontWeight.bold),
                          ),
                          Padding(
                            padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
                            child: Container(
                              width: 210,
                              // height: 20,
                              child: Text(
                                _textValue.text,
                                style: TextStyle(
                                    fontSize: 16, color: Colors.grey[400]),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                    Divider(),
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Row(
                        children: [
                          Text(
                            '总价：',
                            style: TextStyle(
                                fontWeight: FontWeight.bold, fontSize: 16),
                          ),
                          Padding(
                            padding: const EdgeInsets.fromLTRB(16, 0, 16, 0),
                            child: Text(
                              '￥${getAllMoney()}',
                              style: TextStyle(
                                  fontWeight: FontWeight.bold,
                                  color: Colors.blue[800],
                                  fontSize: 16),
                            ),
                          )
                        ],
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.fromLTRB(32, 16, 32, 8),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          RaisedButton(
                            splashColor: Colors.white60,
                            textColor: Colors.purple,
                            color: Colors.white,
                            shape: RoundedRectangleBorder(
                                side:
                                    BorderSide(color: Colors.purpleAccent[700]),
                                borderRadius:
                                    BorderRadius.all(Radius.circular(100))),
                            child: Text(
                              '付款',
                              style: TextStyle(fontSize: 16),
                            ),
                            onPressed: () {
                              print('这是付款按钮');
                            },
                          ),
                          RaisedButton(
                            textColor: Colors.white,
                            color: Colors.purpleAccent[700],
                            shape: RoundedRectangleBorder(
                                side: BorderSide.none,
                                borderRadius:
                                    BorderRadius.all(Radius.circular(100))),
                            child: Text(
                              '退出',
                              style: TextStyle(fontSize: 16),
                            ),
                            onPressed: () {
                              print('这是退出按钮');
                              Navigator.pop(context);
                            },
                          ),
                        ],
                      ),
                    )
                  ],
                ),
              ),
              //  buyForm(),
            );
          });
        }).then((value) => null);
  }

  void postShopBuy() async {
    String content = "";
    // for (var i = 0; i < this.buyList.length; i++) {
    //   content += this.buyList[i]['commodityId'].toString() +
    //       "x" +
    //       this.buyList[i]['commodityNumber'].toString() +
    //       ";";
    // }
    // var response = await Http.post(
    //     'http://xiangrikui.vaiwan.com/shop/buy?content=${content}&money=${getAllMoney()}');
    // // print(response);
    // this.orderNumber = response.toString();
    // print(this.orderNumber);
  }

  void payPage() {}
  submitOrderCartList() {
    List<Widget> list = [];
    // for (var i = 0; i < this.buyList.length; i++) {
    //   list.add(submitOrderCartL(
    //     name: this.buyList[i]['name'],
    //     money: this.buyList[i]['money'],
    //     imgurl1: this.buyList[i]['imgurl1'],
    //     commodityNumber: this.buyList[i]['commodityNumber'],
    //     commodityNumberFun: setCommodityNum,
    //     commodityId: this.buyList[i]['commodityId'],
    //     intro: this.buyList[i]['intro'],
    //   ));
    // }
    return list;
  }

  void setCommodityNum(List huidiaoList) {
    // this.buyList[huidiaoList[0]]['commodityNumber'] = huidiaoList[1];
    print('回调内容');
    // 返回来的用在这里，可在头定义，进行修改
    // this.buyList[]
    // for (var i = 0; i < this.buyList.length; i++) {
    //   if (this.buyList[i]['commodityId'] == huidiaoList[0]) {
    //     this.buyList[i]['commodityNumber'] = huidiaoList[1];
    //   }
    // }
    print(huidiaoList);
    setState(() {});
  }

  int getAllNum() {
    int allNum = 0;
    // for (var i = 0; i < this.buyList.length; i++) {
    //   allNum += this.buyList[i]['commodityNumber'];
    // }
    return allNum;
  }

  int getAllMoney() {
    int allMoney = 0;
    // for (var i = 0; i < this.buyList.length; i++) {
    //   allMoney += this.buyList[i]['money'] * this.buyList[i]['commodityNumber'];
    // }
    return allMoney;
  }
}
